package com.greateamd.timetableasy.server.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.greateamd.timetableasy.domain.Course;
import com.greateamd.timetableasy.domain.StudyPeriod;
import com.greateamd.timetableasy.server.dao.CourseDAO;
import com.greateamd.timetableasy.server.dao.util.AbstractHibernateDAO;



@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED, readOnly = false)	
@Repository("courseDAO")
public class CourseDAOImpl extends AbstractHibernateDAO<Course,Integer> implements CourseDAO {

	public CourseDAOImpl() {
		super(new Course());
	}

	@Override
	public List<Course> getAllOfStudyPeriod(final StudyPeriod sp) {
		logger.debug("Get all study period from study "+sp.getId() +" "+sp.getName());
		HibernateCallback callback = new HibernateCallback() {

			public Object doInHibernate(Session session) throws HibernateException,SQLException {
				Criteria critia = session.createCriteria(Course.class);
				critia.add(Restrictions.eq("studyPeriod", sp));
				critia.add(Restrictions.isNull("promotion"));
				List<Course> result = critia.list();
				return result;
	        }
	    };
	    return (List) getHibernateTemplate().execute(callback);
	}
}
